﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;

namespace MainGame.Menu
{

  public interface IGameSettings
  {
  }

  public class GameSettings : BaseMenu, IGameSettings
  {

    Button square, hintsCheckBox, tutorialCheckBox;
    Button[] difficultyBtns;
    Texture2D check;
    SpriteFont smallFont, baveuseBig;
    string difficultyDescription;
    string[] descriptions;
    Vector2 savedPos, descriptionPos;
    Color colorDisplayString, color;
    public bool Animated = false, hintsChecked, tutorialChecked, backClicked = false, startSettings = false;
    public int Direction = 1, curr_x = 800, alphaChange = 1, AnimVelocity = 160;
    SpriteBatch spriteBatch;


    public GameSettings(Game game, MenuDepths depth)
      : base(game, depth)
    {
      background = null;
      bounds = new Rectangle(Game.GraphicsDevice.Viewport.Bounds.Center.X, 40, 220, 330);
      //game.Services.AddService(typeof(IGameSettings), this);
    }

    protected override void Dispose(bool disposing)
    {
      //Game.Services.RemoveService(typeof(IGameSettings));
      base.Dispose(disposing);
    }

    public override void Initialize()
    {
      spriteBatch = Game.GetContext().SpriteBatch;
      color = Color.Yellow;
      color.R = 150;

      savedPos.X = bounds.X;

      // Load all buttons
      square = new Button(Game, @"Images//Menu//Square", new Rectangle(490, bounds.Y + 15, 150, 45), color, "small_font", "", Color.Blue);
      AddButton(square, () =>
      {
      });

      difficultyBtns = new Button[3];
      for (int i = 0; i < 3; i++) 
      {
        var curr_i = i;

        //buttons used automatically generated, to set distance between the buttons use this formula: 200 +((i + 1) * 130 where 200 is the inizial 'x' and then 130 is the distance between each button
        difficultyBtns[i] = new Button(Game, @"Images//Menu//button2", new Rectangle((int)bounds.X + 340 + ((i + 1) * 130), (int)bounds.Y + 15, 110, 35), Color.Green, "small_font", Locale.Strings.DifficultyEasy, Color.White);
        AddButton(difficultyBtns[i], () =>
        {
          square.bounds.Width = difficultyBtns[curr_i].bounds.Width + 15;
          square.bounds.Height = difficultyBtns[curr_i].bounds.Height + 15;
          square.bounds.X = difficultyBtns[curr_i].bounds.X - 7;
          square.bounds.Y = difficultyBtns[curr_i].bounds.Y - 7;
          difficultyDescription = descriptions[curr_i];
          Game.GetContext().currentConfig.difficulty = (GameDifficulty)curr_i;
        });

        if (i == 1)
        {
          difficultyBtns[i].message = Locale.Strings.DifficultyMedium;
          difficultyBtns[i].color = Color.Orange;
        }
        else if (i == 2)
        {
          difficultyBtns[i].message = Locale.Strings.DifficultyHard;
          difficultyBtns[i].color = Color.Red;
        }
      }


      hintsCheckBox = new Button(Game, @"Images//Menu//CheckBox", new Rectangle((int)bounds.X + 490, (int)bounds.Y + 200, 35, 35), Color.White, "large_font", "", Color.DarkBlue);
      AddButton(hintsCheckBox, () =>
      {
        if (!hintsChecked)
          hintsChecked = true;
        else
          hintsChecked = false;
        Game.GetContext().currentConfig.hints = hintsChecked;
      });


      tutorialCheckBox = new Button(Game, @"Images//Menu//CheckBox", new Rectangle((int)bounds.X + 490, (int)bounds.Y + 260, 35, 35), Color.White, "large_font", "", Color.DarkBlue);
      AddButton(tutorialCheckBox, () =>
      {
        if (!tutorialChecked)
          tutorialChecked = true;
        else
          tutorialChecked = false;
        Game.GetContext().currentConfig.tutorial = tutorialChecked;
      });

      check = Game.Content.Load<Texture2D>(@"Images//Menu//Check");
      smallFont = Game.Content.Load<SpriteFont>("small_font");
      baveuseBig = Game.Content.Load<SpriteFont>("baveuse1_font");

      // Load current settings
      hintsChecked = Game.GetContext().currentConfig.hints;
      tutorialChecked = Game.GetContext().currentConfig.tutorial;

      int savedDifficulty = (int)Game.GetContext().currentConfig.difficulty;
      square.bounds.Width = difficultyBtns[savedDifficulty].bounds.Width + 15;
      square.bounds.Height = difficultyBtns[savedDifficulty].bounds.Height + 15;
      square.bounds.X = difficultyBtns[savedDifficulty].bounds.X - 7;
      square.bounds.Y = difficultyBtns[savedDifficulty].bounds.Y - 7;

      descriptions = new string[3];
      descriptions[0] = Locale.Strings.EasyDiffDesc;
      descriptions[1] = Locale.Strings.MediumDiffDesc;
      descriptions[2] = Locale.Strings.HardDiffDesc.Replace("\\n", "\n");
      difficultyDescription = descriptions[savedDifficulty];
      descriptionPos = new Vector2(bounds.X + 470, bounds.Y + 70);

      base.Initialize();
    }

    public override void Update(GameTime gameTime)
    {

      color.R += (byte)(alphaChange * 10);

      if ((color.R >= 240) || (color.R <= 10))
        alphaChange *= -1;

      square.color = color; 

      if (backClicked)
      {
        foreach (Button b in (from cb in buttons select cb.b))
        {
          b.bounds.X += AnimVelocity - 60;
          colorDisplayString = Color.Transparent;
        }
        descriptionPos.X += AnimVelocity - 60;
      }

      if (startSettings)
      {
        foreach (Button b in (from cb in buttons select cb.b))
        {
          b.bounds.X -= AnimVelocity;

          if (b.bounds.X <= bounds.Left)
          {
            startSettings = false;
            colorDisplayString = Color.Red;
            descriptionPos.X = bounds.X + 150;
          }
        }
        descriptionPos.X -= AnimVelocity;
      }

      base.Update(gameTime);
    }


    public override void Draw(GameTime gameTime)
    {
      base.Draw(gameTime);

      spriteBatch.Begin();

      spriteBatch.DrawString(smallFont, difficultyDescription, descriptionPos, Color.White);

      if (hintsChecked)
        spriteBatch.Draw(check, new Rectangle(hintsCheckBox.bounds.Center.X - 16, hintsCheckBox.bounds.Top - 10, 40, 40), colorDisplayString);

      if (tutorialChecked)
        spriteBatch.Draw(check, new Rectangle(tutorialCheckBox.bounds.Center.X - 16, tutorialCheckBox.bounds.Top - 10, 40, 40), colorDisplayString);

      spriteBatch.DrawString(baveuseBig, Locale.Strings.Hints, new Vector2((int)bounds.X + 55, bounds.Y + 190), colorDisplayString);
      spriteBatch.DrawString(baveuseBig, Locale.Strings.Tutorial, new Vector2((int)bounds.X + 55, bounds.Y + 250), colorDisplayString);
      

      spriteBatch.End();
    }

    //public event MenuEvent OnGraphicsSettings;
    //public event MenuEvent OnAudioSettings;
  }
}
